package sky.cin.framework.sfoc.core.interfaces;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public interface BeanInterfaceFactory extends MethodInterceptor {
        
        default <T> T newProxy(Class<?> clazz, BeanInterfaceFactory instance) {
            if (!beforeProxyCreat(clazz)) {
                return null;
            }
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(clazz);
            enhancer.setCallback(instance);
            return (T) proxyCreated(enhancer.create());
        }
    
        default Class<?> resolveSourceClass(Object bean) {
            String classPath = bean.getClass().toString();
            int endIndex = classPath.indexOf("$$");
            classPath = classPath.substring(6, endIndex);
            Class<?> clazz = null;
            try {
                clazz = Class.forName(classPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return clazz;
        }
        
        boolean beforeProxyCreat(Class<?> clazz);
        
        Object proxyCreated(Object bean);
        
        Object intercept(Object bean, Method method, Object[] args, MethodProxy methodProxy) throws Throwable;
        
}
